<template>
  <div class="danye">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>酒类型管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 添加按钮 -->
    <el-button style="margin-top: 1rem;" type="primary" @click="showAddDialog">添加酒类型</el-button>

    <!-- 酒类型分类表格 -->
    <el-table :data="wineCategories" style="width: 100%">
      <el-table-column prop="categoryId" label="ID"></el-table-column>
      <el-table-column prop="name" label="酒类型"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <!-- 编辑按钮 -->
          <el-button type="text" @click="showEditDialog(scope.row)"
            >编辑</el-button
          >
          <!-- 删除按钮 -->
          <el-button type="text" @click="handleDelete(scope.row.categoryId)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加/编辑对话框 -->
    <el-dialog
      :visible.sync="dialogVisible"
      :title="dialogTitle"
      @close="handleCloseDialog"
    >
      <el-form
        ref="wineCategoryForm"
        :model="currentWineCategory"
        label-width="80px"
      >
        <el-form-item label="酒类型" prop="name">
          <el-input v-model="currentWineCategory.name"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCloseDialog">取消</el-button>
        <el-button type="primary" @click="handleSave">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { winelist, addWine, deleteWine, updateWine } from "@/api/winetype";
import { Message } from "element-ui";
export default {
  data() {
    return {
      wineCategories: [], // 酒类型分类数据
      dialogVisible: false, // 对话框显示状态
      currentWineCategory: {
        // 当前编辑的酒类型分类
        categoryId: null,
        name: "",
      },
      currentPage: "",
      pageSize: "",
      searchKeyword: "",
      mode: "",
    };
  },
  methods: {
    // 显示添加对话框
    showAddDialog() {
      this.dialogVisible = true;
      this.currentWineCategory = { categoryId: null, name: "" };
      this.mode = "add";
    },
    // 显示编辑对话框
    showEditDialog(row) {
      this.dialogVisible = true;
      this.currentWineCategory = { ...row };
      this.mode = "edite";
    },
    // 关闭对话框
    handleCloseDialog() {
      this.dialogVisible = false;
    },
    // 保存酒类型分类
    handleSave() {
      // 调用后端接口保存数据，示例中未实现
      // 保存成功后，刷新酒类型分类数据
      if (this.mode === "add") {
        this.addWine();
      } else if (this.mode === "edite") {
        this.editeWine();
      }
    },

    async editeWine() {
      const param = {
        categoryId: this.currentWineCategory.categoryId,
        name: this.currentWineCategory.name
      }
      const res = await updateWine(param);
      if (res.code == 200) {
        Message("修改成功");
        this.loadWineCategories();
        this.handleCloseDialog();
      } else {
        Message("修改失败");
      }
    },
    async addWine() {
      const param = {
        name: this.currentWineCategory.name
      }
      const res = await addWine(param);
      if (res.code == 200) {
        Message("保存成功");
        this.loadWineCategories();
        this.handleCloseDialog();
      } else {
        Message("保存失败");
      }
    },
    // 删除酒类型分类
    async handleDelete(categoryId) {
      // 调用后端接口删除数据，示例中未实现
      // 删除成功后，刷新酒类型分类数据
      const res = await deleteWine(categoryId);
      if (res.code == 200) {
        Message("删除成功");
        this.loadWineCategories();
      } else {
        Message("删除失败");
      }
    },
    // 加载酒类型分类数据，示例中未实现
    async loadWineCategories() {
      // 调用后端接口获取酒类型分类数据，示例中未实现
      // 将获取到的数据赋值给 this.wineCategories
      const res = await winelist(
        this.currentPage,
        this.pageSize,
        this.searchKeyword
      );
      if (res.code == 200) {
        this.wineCategories = res.data.list.records;
        // this.pageSize = res.data.user.size;
        // this.currentPage = res.data.user.current;
        // this.totalWines = res.data.user.total;
      }
    },
  },
  computed: {
    dialogTitle() {
      return this.mode === "add" ? "添加类型" : "编辑类型";
    },
  },
  created() {
    // 初始化加载酒类型分类数据
    this.loadWineCategories();
  },
};
</script>

<style scoped>
/* 添加适当的样式以美化页面 */
</style>
